﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;

public partial class test : System.Web.UI.Page
{
    IRolePurviewHandler rolePurviewManage = new RolePurviewHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //if (Session["UserID"] != null)
            //{
            //    ISystemHandler sysManage = new SystemHandler();
            //    string strPermission = sysManage.GetLoginUserPermissions();
            //    if (!strPermission.Contains("1001"))
            //    {
            //        Response.Redirect("~/NotPurview.aspx");
            //        return;
            //    }
            //}

//            HK2009.Common.SendMail send = new SendMail();
//            send.MailBody = "this is a test";
////            send.MailFrom = "server@special-line.com";
//            send.MailSubject = "dds";
//            send.MailTo = "ruanpeng8@163.com";
//            send.Send();

             //string aa= HK2009.Common.DesEncryption.Encrypt("25418884");
            ViewState["OrderBy"] = "PurviewID";
            ViewState["Accending"] = "asc";
            dataBind();

            //DataBase db = new DataBase();
            //SqlParameter[] parms=new SqlParameter[6];
            //parms[0] = new SqlParameter("@JobDateFrom", "1900-01-01 00:00:00");
            //parms[1] = new SqlParameter("@JobDateTo","2100 23:59:59");
            //parms[2] = new SqlParameter("@Shipper","%");
            //parms[3] = new SqlParameter("@Vendor", "%");
            //parms[4] = new SqlParameter("@Whse", "%");
            //parms[5] = new SqlParameter("@LinceseNo", "%");
            //db.RunSpTab("proc_TruckStatus", parms);
        }
    }

    private void ProcessParms(ref string sqlstr, ref SqlParameter[] parms)
    {
        // parms = new SqlParameter[2];
        int i = 0;
        sqlstr = "";
        if (ViewState["PurviewName"] != null)
        {
            parms[0] = new SqlParameter("@PurviewName", ViewState["PurviewName"].ToString());
            sqlstr += " PurviewName like '%' +  @PurviewName + '%'";
        }
        else
        {
            parms[0] = new SqlParameter("@PurviewID" + i.ToString(), "-1");
            sqlstr += " PurviewID != @PurviewID" + i.ToString();
            i += 1;
        }

        if (ViewState["PurviewGroupCode"] != null)
        {
            parms[1] = new SqlParameter("@purviewGroupCode", ViewState["PurviewGroupCode"].ToString());
            sqlstr += " and PurviewGroupCode=@PurviewGroupCode";
        }
        else
        {
            parms[1] = new SqlParameter("@PurviewID" + i.ToString(), "-1");
            sqlstr += " and PurviewID!=@PurviewID" + i.ToString();
        }
    }

    private void dataBind()
    {
        string sqlStr = "SELECT * FROM Purviews where ";
        SqlParameter[] parms = new SqlParameter[2];
        string clause = string.Empty;
        ProcessParms(ref clause, ref parms);
        sqlStr += clause;

        DataTable dt = rolePurviewManage.GetPurviewList(sqlStr, parms);
        DataView view = dt.DefaultView;
        view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
        gvPurview.DataSource = view;
        gvPurview.DataBind();
        //if (dt != null && dt.Rows.Count > 0)
        //{
        //    lblTotal.Text = dt.Rows.Count.ToString();
        //}
        //else
        //{
        //    lblTotal.Text = "0";
        //}
    }


    protected void gvPurview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPurview.PageIndex = e.NewPageIndex;
        dataBind();
    }
    protected void gvPurview_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void gvPurview_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }
   // protected void btnSearch_Click(object sender, EventArgs e)
    //{
    //    if (txtPurviewName.Text.Trim() != string.Empty)
    //    {
    //        ViewState["PurviewName"] = txtPurviewName.Text.Trim();
    //    }
    //    else
    //    {
    //        ViewState["PurviewName"] = null;
    //    }
    //    if (txtPurviewGroup.Text.Trim() != string.Empty)
    //    {
    //        ViewState["PurviewGroupCode"] = txtPurviewGroup.Text.Trim();
    //    }
    //    else
    //    {
    //        ViewState["PurviewGroupCode"] = null;
    //    }

    //    dataBind();
  //  }
    //protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    gvPurview.PageSize = int.Parse(ddlPages.SelectedValue);
    //    dataBind();
    //}
    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvPurview.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvPurview.PageCount)
            {
                this.gvPurview.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax("请输入大于0小于总页数的正整数!", UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax("请输入大于0小于总页数的正整数!", UpdatePanel1);
        }
    }

    protected void dds_Click(object sender, EventArgs e)
    {
        string aa = string.Empty;
        for (int i = 0; i < gvPurview.Rows.Count; i++)
        {
            HtmlInputCheckBox chk =(HtmlInputCheckBox)gvPurview.Rows[i].FindControl("chkSelected");
            bool isChecked = chk.Checked;
            if (isChecked)
            {
                aa += chk.Value+",";
            }           

        }

        Jscript.AlertInAjax(aa, UpdatePanel1);
    }
    protected void gvPurview_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvPurview.EditIndex = e.NewEditIndex;
        dataBind();
    }
    protected void gvPurview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lblID = (Label)gvPurview.Rows[e.RowIndex].FindControl("lblID");
        TextBox txtPurviewName = (TextBox)gvPurview.Rows[e.RowIndex].FindControl("txtPurviewName");

        //已经获取了ID和更改了的字段，自己写个更新方法更新上去
        //
        gvPurview.EditIndex = -1;
        dataBind();
    }
    protected void gvPurview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvPurview.EditIndex = -1;
        dataBind();
    }
    protected void gvPurview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
            rb.Attributes.Add("onclick", "judge(this)");//给RadioButton添加onclick属性
        }

    }
    protected void radiobutton1_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvPurview.Rows)
        {
            RadioButton rad = (RadioButton)row.FindControl("radiobutton1");
            HiddenField hidPKID = (HiddenField)row.FindControl("hidValue");
            if (rad.Checked)
            {
                lblTest.Text = hidPKID.Value;
            }
        }
    }
}
